package com.example.demo.huawei;

import java.util.HashMap;
import java.util.Map;

public class Main14 {

    public static void main(String[] args) {
        System.out.println(new Main14().lengthOfLongestSubstring("abcabcbb"));
    }

    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int begin = 0;
        int maxLength = 0;
        for (int end = 0; end < s.length(); end++) {
            char ch = s.charAt(end);
            if (map.containsKey(ch)) {
                begin = Math.max(begin, map.get(ch) + 1);
                map.put(ch, end);
            } else {
                map.put(ch, end);
            }
            //            System.out.println(s.substring(begin, end + 1));
            maxLength = Math.max(maxLength, end - begin + 1);
        }

        return maxLength;
    }
}
